<?php


namespace app\pc\service\base;


use app\admin\model\base\Paper;
use app\admin\model\base\Practise;
use app\admin\model\base\StudentPaper;
use app\BaseService;
use think\response\Json;

class IndexService extends BaseService
{
    /**
     * 首页大厅数据
     * @return Json
     */
    public function index($params): Json
    {
        //获取试卷
        $list = Paper::where('is_del','=',0)
            ->where('is_release','=',1)
            ->field('student,id')
            ->select()->toArray();
        $arr = [];
        $arr[] = 0;
        if(!empty($list)){
            foreach ($list as $value){
                if(is_string($value['student'])){
                    $a = json_decode($value['student']);
                    if(in_array(PC_USER_ID,$a)){
                        $arr[] = $value['id'];
                    }
                }
            }
        }
        $where = [];
        $where[] = ['student_id','=',PC_USER_ID];
        //查询用户是否参加考试
        $list = StudentPaper::where($where)->select()->toArray();
        $arrs = [];
        $ids = [0];//未参加
        if(!empty($list)){
            foreach ($list as $item){
                $arrs[$item['paper_id']] = $item;
                $ids[] = $item['paper_id'];
            }
        }
        //刷选未参加的
        $ids = array_values(array_unique($ids));
        $arr = array_diff($arr,$ids);
        if(empty($arr)) $arr = [0];
        $arr = array_values($arr);
        $where2 = [];
        $where2[] = ['is_del','=',0];
        $where2[] = ['is_release','=',1];
        $where2[] = ['id','in',implode(',',$arr)];
        $paperList = Paper::where($where2)
            ->with(['paper_subjects','special'])
//            ->where('start_time','<',date('Y-m-d H:i:s',time()))
//            ->where('end_time','>',date('Y-m-d H:i:s',time()))
            ->order('end_time','asc')
            ->limit(4)
            ->select()->toArray();
        $paperListArr = [];
        if(!empty($paperList)){
            foreach ($paperList as $item){
                $subject_num = count($item['paper_subjects']);
                $subject_score = 0;
                if(!empty($item['paper_subjects'])){
                    foreach ($item['paper_subjects'] as $subject){
                        $subject_score+= $subject['weighting'];
                    }
                }
                $paperListArr[] = [
                    'name'=>$item['name'],
                    'end_time'=>$item['end_time'],
                    'subject_num'=>$subject_num,
                    'subject_score'=>$subject_score,
                    'img_url'=>$item['img_url'],
                    'paper_id'=>$item['id'],
                    'special_name'=>$item['special']['name'],
                ];
            }
        }

        //获取练习
        $practiseList = Practise::where('is_del','=',0)
            ->where('is_release','=',1)
            ->with(['practise_subject','special'])
//            ->where('start_time','<',date('Y-m-d H:i:s',time()))
//            ->where('end_time','>',date('Y-m-d H:i:s',time()))
            ->order('end_time','asc')
            ->limit(4)
            ->select()->toArray();
        $practiseListArr = [];
        if(!empty($practiseList)){
            foreach ($practiseList as $item){
                $subject_num = count($item['practise_subject']);
                $subject_score = 0;
                if(!empty($item['practise_subject'])){
                    foreach ($item['practise_subject'] as $subject){
                        $subject_score+= $subject['weighting'];
                    }
                }
                $practiseListArr[] = [
                    'name'=>$item['name'],
                    'end_time'=>$item['end_time'],
                    'subject_num'=>$subject_num,
                    'subject_score'=>$subject_score,
                    'img_url'=>$item['img_url'],
                    'practise_id'=>$item['id'],
                    'special_name'=>$item['special']['name'],
                ];
            }
        }
        return jsonReturn(1, ['paper'=>$paperListArr,'practise'=>$practiseListArr]);
    }
}